import { config } from '../config'

const tips = {
    1: '抱歉，出现了一个错误',
    2: '网络异常',
    1005: 'appkey无效，请前往www.7yue.pro申请',
    3000: '期刊不存在'
}

class HTTP {
    request ({ url, data = {}, method = 'GET' }) {
        return new Promise((resolve, reject) => {
            this._request(url, resolve, reject, data, method)
        })
    }
    _request (url, resolve, reject, data = {}, method = 'GET') {
        wx.request({
            data,
            method,
            url: config.api_base_url + url,
            header: {
                'content-type': 'application/json',
                appkey: config.appkey
            },
            success: (res) => {
                const code = res.statusCode.toString()
                if (code.startsWith('2')) {
                    resolve(res)
                } else {
                    reject()
                    const { error_code } = res.data
                    this._show_error(error_code)
                }
            },
            fail: (err) => {
                reject()
                this._show_error(2)
            }
        })
    }

    _show_error (error_code) {
        if (!error_code) {
            error_code = 1
        }
        const tip = tips[error_code]
        wx.showToast({
            title: tip || tips[1],
            icon: 'none',
            duration: 20000
        })
    }
}

export { HTTP }
